package com.example;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.IOException;

// 老师界面控制器，暂时为空
public class TeacherController {

    @FXML
    private Button backButton;
    @FXML
    private AnchorPane contentArea;

    private Stage primaryStage;
    private User currentUser;

    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    public void setCurrentUser(User user) {
        this.currentUser = user;
        System.out.println("TeacherController received user: " + (user != null ? user.getUsername() : "null"));
    }

    // 课程管理相关方法
    @FXML
    private void handleViewCourses() {
        loadSubScene("TeacherTaughtCourses.fxml", "查看所授课程列表");
    }

    @FXML
    private void handleMaintainCourseInfo() {
        loadSubScene("TeacherMaintainCourseInfo.fxml", "维护课程信息");
    }

    @FXML
    private void handleUploadCourseMaterials() {
        loadSubScene("TeacherUploadMaterials.fxml", "上传课程资料");
    }

    // 成绩管理相关方法
    @FXML
    private void handleInputGrades() {
        loadSubScene("TeacherRecordGrade.fxml", "录入学生成绩");
    }

    @FXML
    private void handleImportGrades() {
        loadSubScene("TeacherImportGrades.fxml", "批量导入成绩");
    }

    @FXML
    private void handleReviewGrades() {
        loadSubScene("TeacherReviewGrades.fxml", "成绩审核与确认");
    }

    @FXML
    private void handleMarkGradeExceptions() {
        loadSubScene("TeacherMarkExceptions.fxml", "成绩异常标记");
    }
    
    @FXML
    private void handleViewStudentGrades() {
        loadSubScene("TeacherViewStudentGrades.fxml", "查看学生成绩");
    }

    // 学生管理相关方法
    @FXML
    private void handleViewClassStudents() {
        loadSubScene("TeacherViewClassStudents.fxml", "查看班级学生名单");
    }

    @FXML
    private void handleQueryStudentInfo() {
        loadSubScene("TeacherQueryStudentInfo.fxml", "查询学生基本信息");
    }

    @FXML
    private void handleExportGradeReport() {
        loadSubScene("TeacherExportGradeReport.fxml", "导出学生成绩报表");
    }

    // 统计分析相关方法
    @FXML
    private void handleGradeDistribution() {
        loadSubScene("TeacherGradeDistribution.fxml", "课程成绩分布统计");
    }

    @FXML
    private void handleGradeAnalysis() {
        loadSubScene("TeacherGradeAnalysis.fxml", "成绩分析");
    }

    @FXML
    private void handleClassComparison() {
        loadSubScene("TeacherClassComparison.fxml", "班级成绩对比");
    }

    // 返回登录界面
    @FXML
    private void handleBackButton() {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("Login.fxml"));
            Parent root = loader.load();
            
            LoginController loginController = loader.getController();
            Stage stage = (Stage) backButton.getScene().getWindow();
            loginController.setPrimaryStage(stage);
            
            Scene scene = new Scene(root);
            stage.setTitle("登录");
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 加载子场景的通用方法
    private void loadSubScene(String fxmlFile, String title) {
        try {
            System.out.println("TeacherController: Loading subscene: " + fxmlFile);
            FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
            Parent root = loader.load();

            // 如果子界面实现了TeacherSubController接口，设置primaryStage和teacherController
            if (loader.getController() instanceof TeacherSubController) {
                TeacherSubController subController = (TeacherSubController) loader.getController();
                subController.setPrimaryStage(primaryStage);
                subController.setTeacherController(this);

                // 如果子控制器需要当前用户，传递用户对象
                System.out.println("TeacherController: Passing currentUser to subController: " + (currentUser != null ? currentUser.getUsername() : "null"));
                subController.setCurrentUser(currentUser); // 所有TeacherSubController都需要currentUser
            }

            contentArea.getChildren().clear();
            contentArea.getChildren().add(root);

            if (primaryStage != null) {
                primaryStage.setTitle("教师界面 - " + title);
            }
        } catch (Exception e) {
            e.printStackTrace();
            contentArea.getChildren().clear();
            contentArea.getChildren().add(new javafx.scene.control.Label(title + "功能正在开发中..."));
        }
    }
} 